﻿namespace WhoCanHelpMe.Web.Controllers.Home.Mappers
{
    #region Using Directives

    using System.Collections.Generic;
    using System.Linq;

    using AutoMapper;

    using Contracts;

    using Domain;

    using Shared.Mappers.Contracts;

    using ViewModels;

    #endregion

    public class HomePageViewModelMapper : IHomePageViewModelMapper
    {
        private readonly IPageViewModelBuilder pageViewModelBuilder;

        public HomePageViewModelMapper(IPageViewModelBuilder pageViewModelBuilder)
        {
            this.pageViewModelBuilder = pageViewModelBuilder;
            Mapper.CreateMap<NewsItem, NewsItemViewModel>();
        }

        public HomePageViewModel MapFrom(IList<NewsItem> input)
        {
            var viewModel = new HomePageViewModel
                {
                    NewsItems =
                        input.ToList().ConvertAll(
                        i => Mapper.Map<NewsItem, NewsItemViewModel>(i))
                };

            return this.pageViewModelBuilder.UpdateSiteProperties(viewModel);
        }
    }
}